/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * WithdrawalPanel.java
 *
 * Created on 22-Mar-2011, 6:29:26 AM
 */

package com.bullionxpress.client.view;

import com.bullionxpress.client.application.ClientGlobals;
import com.bullionxpress.shared.model.Account;
import com.bullionxpress.shared.service.AccountNotFoundException;
import com.bullionxpress.shared.service.AdminService;
import com.bullionxpress.shared.service.InsufficientFundingException;
import com.bullionxpress.shared.service.InvalidAmountException;
import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDialog;
import javax.swing.JOptionPane;

/**
 *
 * @author BullionXpress.com
 */
public class WithdrawalPanel extends javax.swing.JPanel {

    /** Creates new form WithdrawalPanel */
    JDialog owner = null;
    public WithdrawalPanel(JDialog owner) {
        this.owner = owner;
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        amountField = new java.awt.TextField();
        jLabel2 = new javax.swing.JLabel();
        withdrawalButton = new javax.swing.JToggleButton();
        cancelButton = new javax.swing.JButton();
        accountField = new javax.swing.JTextField();

        jLabel1.setText("Withdrawal Amount");

        jLabel2.setText("Account");

        withdrawalButton.setText("Withdrawal");
        withdrawalButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                withdrawalButtonActionPerformed(evt);
            }
        });

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1)
                    .addComponent(amountField, javax.swing.GroupLayout.DEFAULT_SIZE, 160, Short.MAX_VALUE)
                    .addComponent(accountField, javax.swing.GroupLayout.DEFAULT_SIZE, 160, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(cancelButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(withdrawalButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addGap(3, 3, 3)
                .addComponent(accountField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addGap(2, 2, 2)
                .addComponent(amountField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(withdrawalButton)
                    .addComponent(cancelButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        // TODO add your handling code here:
        owner.dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void withdrawalButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_withdrawalButtonActionPerformed
        try {
            // TODO add your handling code here:
            AdminService as = ClientGlobals.adminService;
            String accountId = accountField.getText();
            Account before = ClientGlobals.accountDatabase.get(accountId);
            double amount = Double.parseDouble(amountField.getText());
            as.withdrawal(accountId, amount);
            Account after = ClientGlobals.accountDatabase.get(accountId);
            if( after.getBalance() != (before.getBalance() - amount) ){
                JOptionPane.showMessageDialog(owner, "Withdrawal unsuccessful");
            } else {
                JOptionPane.showMessageDialog(owner, "Withdrawal successful");
            }
        } catch (InsufficientFundingException ex) {
            Logger.getLogger(WithdrawalPanel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (AccountNotFoundException ex) {
            Logger.getLogger(WithdrawalPanel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvalidAmountException ex) {
            Logger.getLogger(WithdrawalPanel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (RemoteException ex) {
            Logger.getLogger(WithdrawalPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_withdrawalButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField accountField;
    private java.awt.TextField amountField;
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JToggleButton withdrawalButton;
    // End of variables declaration//GEN-END:variables

}
